package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.base.BaseActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/19.
 */

public class MapActivity extends BaseActivity implements AMap.InfoWindowAdapter {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.map_view)
    MapView mMapView;

    private double lat = 0, lng = 0;
    View infoWindow = null;
    AMap aMap = null;
    private Marker marker;
    private String area;
    private CameraUpdate mUpdata;

    @Override
    protected int getContentView() {
        return R.layout.activity_map;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lat = getIntent().getDoubleExtra("lat", 0d);
        lng = getIntent().getDoubleExtra("lng", 0d);
        area = getIntent().getStringExtra("area");

        if (aMap == null) {
            aMap = mMapView.getMap();
        }

        mMapView.onCreate(savedInstanceState);

        aMap.setInfoWindowAdapter(this);//AMap类中
        LatLng latLng = new LatLng(lat, lng);

        mUpdata = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 16, 0, 30));//这是地理位置，就是经纬度。
        aMap.moveCamera(mUpdata);//定位的方法
        marker = aMap.addMarker(new MarkerOptions().position(latLng).snippet(area));
        marker.showInfoWindow();

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        if (mMapView != null) {
            mMapView.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        if (mMapView != null) {
            mMapView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        if (mMapView != null) {
            mMapView.onPause();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        if (mMapView != null) {
            mMapView.onSaveInstanceState(outState);
        }
    }

    @Override
    public View getInfoWindow(Marker marker) {
        if (infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(
                    R.layout.item_info_window, null);
        }
        TextView tv_locatiom = infoWindow.findViewById(R.id.tv_location);
        tv_locatiom.setText(area);
        TextView to_go = infoWindow.findViewById(R.id.go);
        to_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invokingGD(area);
            }
        });

        return infoWindow;
    }

    public void invokingGD(String location) {

        //  com.autonavi.minimap这是高德地图的包名
        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse("androidamap://navi?sourceApplication=应用名称&lat=" + "&dev=0"));
        intent.setPackage("com.autonavi.minimap");
        intent.setData(Uri.parse("androidamap://poi?sourceApplication=softname&keywords=" + location));

        if (isInstallByread("com.autonavi.minimap")) {
            startActivity(intent);
            Log.e("GasStation", "高德地图客户端已经安装");
        } else {
            Toast.makeText(mContext, "没有安装高德地图客户端", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 判断是否安装目标应用
     *
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }


    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}
